/**
 *====================================================
 * 文件名称: EnterpriseCastor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年9月24日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.member.action.v1.dto;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yuexunit.account.action.v2.AccountActionApi;
import com.yuexunit.account.action.v2.model.AccountAlias;
import com.yuexunit.account.action.v2.model.AccountNameType;
import com.yuexunit.core.BusinessException;
import com.yuexunit.core.ConfigPropertites;
import com.yuexunit.finance.bulkstock.entity.BulkstockInfoEntity;
import com.yuexunit.finance.bulkstock.service.BulkstockInfoService;
import com.yuexunit.finance.bulkstock.service.TemporaryCreditLineService;
import com.yuexunit.finance.bulkstock.service.TemporaryCreditlineEvaluationService;
import com.yuexunit.finance.common.httpclient.HttpClientHelper;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.context.FinanceSessionContext;
import com.yuexunit.finance.member.action.v1.model.CommissionScaleTable;
import com.yuexunit.finance.member.action.v1.model.DaysEarningBaseDetail;
import com.yuexunit.finance.member.action.v1.model.EarningSummaryBaseDetail;
import com.yuexunit.finance.member.action.v1.model.EnterpriseBaseDetail;
import com.yuexunit.finance.member.action.v1.model.EnterpriseBaseInfo;
import com.yuexunit.finance.member.action.v1.model.EnterpriseDetail;
import com.yuexunit.finance.member.action.v1.model.EnterpriseGradeDetail;
import com.yuexunit.finance.member.action.v1.model.EnterpriseGradeEvaluation;
import com.yuexunit.finance.member.action.v1.model.EnterpriseGradeEvaluationDetail;
import com.yuexunit.finance.member.action.v1.model.FinanceBulkstockPledgeRateDetail;
import com.yuexunit.finance.member.action.v1.model.FinanceFollowInfoDetail;
import com.yuexunit.finance.member.action.v1.model.FinanceFollowPersonDetail;
import com.yuexunit.finance.member.action.v1.model.TemporaryCreditlineDetail;
import com.yuexunit.finance.member.action.v1.model.TemporaryCreditlineEvaluation;
import com.yuexunit.finance.member.action.v1.model.TemporaryCreditlineEvaluationDetail;
import com.yuexunit.finance.member.entity.BankcodeEntity;
import com.yuexunit.finance.member.entity.CommissionScaleTableEntity;
import com.yuexunit.finance.member.entity.EnterpriseCodeEntity;
import com.yuexunit.finance.member.entity.EnterpriseEntity;
import com.yuexunit.finance.member.entity.EnterpriseGradeEntity;
import com.yuexunit.finance.member.entity.EnterpriseGradeEvaluationEntity;
import com.yuexunit.finance.member.entity.EnterpriseModifyEntity;
import com.yuexunit.finance.member.entity.FinanceBulkstockPledgeRateEntity;
import com.yuexunit.finance.member.entity.FinanceFollowInfoEntity;
import com.yuexunit.finance.member.entity.FinanceFollowPersonEntity;
import com.yuexunit.finance.member.entity.MemberGradeEntity;
import com.yuexunit.finance.member.entity.SupplierEntity;
import com.yuexunit.finance.member.entity.TemporaryCreditLineEntity;
import com.yuexunit.finance.member.entity.TemporaryCreditlineEvaluationEntity;
import com.yuexunit.finance.member.entity.lexicon.EnterpriseTypeEnum;
import com.yuexunit.finance.member.service.CommissionScaleTableService;
import com.yuexunit.finance.member.service.EnterpriseCodeService;
import com.yuexunit.finance.member.service.EnterpriseGradeEvaluationService;
import com.yuexunit.finance.member.service.EnterpriseGradeService;
import com.yuexunit.finance.member.service.EnterpriseService;
import com.yuexunit.finance.member.service.FinanceBulkstockPledgeRateService;
import com.yuexunit.finance.member.service.FinanceFollowInfoService;
import com.yuexunit.finance.member.service.FinanceFollowPersonService;
import com.yuexunit.finance.member.service.MemberGradeService;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.finance.statement.entity.DaysEarningEntity;
import com.yuexunit.finance.statement.entity.EarningSummaryEntity;
import com.yuexunit.finance.system.entity.UserCardInfoEntity;
import com.yuexunit.finance.system.service.UserCardInfoService;
import com.yuexunit.finance.zhanghuguangli.entity.EnterpriseAccountEntity;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.util.DateUtil;
/**
 * @ClassName: EnterpriseCastor
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年9月24日 下午10:34:49
 */
public class EnterpriseCastor {
	private static Logger logger = LoggerFactory.getLogger(EnterpriseCastor.class);

	public static EnterpriseEntity fromEnterpriseBaseInfo(EnterpriseBaseInfo info, EnterpriseEntity enterprise, String... ignores) {
		EnterpriseEntity entity = null;
		if (enterprise != null) {
			entity = enterprise;
		} else {
			entity = new EnterpriseEntity();
		}
		BeanUtils.copyProperties(info, entity, ignores);
		entity.setTenantId(FinanceSessionContext.getAccountTenantId());
		entity.setAccountId(FinanceSessionContext.getAccountId());
		entity.setEnterpriseType(EnterpriseTypeEnum.valueOf(info.getEnterpriseType()));

		AccountActionApi accountAction = ComponentFactory.loadClass(AccountActionApi.class);
		List<AccountAlias> accountAlias = accountAction.inquireAccountAliasList(FinanceSessionContext.getAccountId());
		for (AccountAlias a : accountAlias) {
			if (AccountNameType.MOBILE == a.getType()) {
				entity.setMobile(a.getAccountName());
			}
		}
		return entity;
	}
	public static EnterpriseDetail toEnterpriseDetail(EnterpriseEntity entity) {
		FinanceFollowPersonService financeFollowPersonService = ComponentFactory.loadClass(FinanceFollowPersonService.class);
		CommissionScaleTableService commissionScaleTableService = ComponentFactory.loadClass(CommissionScaleTableService.class);
		EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
		FinanceBulkstockPledgeRateService financeBulkstockPledgeRateService = ComponentFactory.loadClass(FinanceBulkstockPledgeRateService.class);
		BulkstockInfoService bulkstockInfoService = ComponentFactory.loadClass(BulkstockInfoService.class);
		EnterpriseDetail detail = new EnterpriseDetail();
		BeanUtils.copyProperties(entity, detail);
		detail.setAuditState(entity.getAuditState().ordinal());
		detail.setState(entity.getState().ordinal());
		detail.setAuthentication(entity.getAuthentication().ordinal());
		detail.setApproveType(entity.getApproveType().ordinal());
		if (entity.getEnterpriseType() != null) {
			detail.setEnterpriseType(entity.getEnterpriseType().ordinal());
		}

		EnterpriseCodeService enterpriseCodeService = ComponentFactory.loadClass(EnterpriseCodeService.class);
		EnterpriseCodeEntity enterpriseCode = enterpriseCodeService.inquireEnterpriseCodeDetail(null, entity.getEnterpriseId());

		if (null != enterpriseCode) {
			detail.setEnterpriseCode(enterpriseCode.getEnterpriseCode());
		}

		if (StringUtils.isNotBlank(entity.getBelongsEnterpriseCode())) {
			EnterpriseCodeEntity ece = enterpriseCodeService.inquireEnterpriseCodeDetail(entity.getBelongsEnterpriseCode(), null);
			if (ece != null) {
				EnterpriseEntity ee = enterpriseService.inquireEnterprise(ece.getEnterpriseId());
				detail.setBelongsEnterprise(ee.getEnterpriseName());
			}
		}

		EnterpriseGradeService enterpriseGradeService = ComponentFactory.loadClass(EnterpriseGradeService.class);
		EnterpriseGradeEntity enterpriseGrade = enterpriseGradeService.inquireEnterpriseGrade(entity.getEnterpriseId());
		EnterpriseGradeEvaluationService enterpriseGradeEvaluationService = ComponentFactory.loadClass(EnterpriseGradeEvaluationService.class);
		EnterpriseGradeEvaluationEntity egee = null;
		if (enterpriseGrade != null) {
			egee = enterpriseGradeEvaluationService.inquireEnterpriseGradeEvaluationDetail(enterpriseGrade.getGradeEvaluationId());
			EnterpriseGradeDetail enterpriseGradeDetail = new EnterpriseGradeDetail();
			BeanUtils.copyProperties(enterpriseGrade, enterpriseGradeDetail);
			enterpriseGradeDetail.setCreditLineRemains(enterpriseGrade.getCreditLine().subtract(enterpriseGrade.getCreditAlready()));
			enterpriseGradeDetail.setEffectiveEndDate(DateUtil.formatDate(enterpriseGrade.getEffectiveEndDate()));
			enterpriseGradeDetail.setEffectiveStartDate(DateUtil.formatDate(enterpriseGrade.getEffectiveStartDate()));
			detail.setEnterpriseGradeDetail(enterpriseGradeDetail);
		}
		EnterpriseGradeEvaluationDetail eged = new EnterpriseGradeEvaluationDetail();
		if (egee != null) {
			eged = EnterpriseCastor.toEnterpriseGradeEvaluationDetail(egee);
			List<FinanceBulkstockPledgeRateEntity> financeBulkstockPledgeRateList = financeBulkstockPledgeRateService.inquireFinanceBulkstockPledgeRateList(egee.getGradeEvaluationId());
			List<FinanceBulkstockPledgeRateDetail> financeBulkstockPledgeRateDetailList = new ArrayList<>();
			for (FinanceBulkstockPledgeRateEntity fbpre : financeBulkstockPledgeRateList) {
				FinanceBulkstockPledgeRateDetail d = new FinanceBulkstockPledgeRateDetail();
				BeanUtils.copyProperties(fbpre, d);
				d.setEffectiveEndDate(DateUtil.formatDate(fbpre.getEffectiveEndDate()));
				d.setEffectiveStartDate(DateUtil.formatDate(fbpre.getEffectiveStartDate()));
				BulkstockInfoEntity bie = bulkstockInfoService.inquireBulkstockInfo(fbpre.getBulkstockInfoId());
				d.setBulkstockName(bie.getBulkstockName());
				d.setSpecificationName(bie.getSpecificationName());
				d.setProductName(fbpre.getProductName());
				d.setProductType((fbpre.getProductType() == null ? ProductTypeEnum.UNKNOWN.ordinal() : fbpre.getProductType().ordinal()));
				financeBulkstockPledgeRateDetailList.add(d);
			}
			eged.setFinanceBulkstockPledgeRateList(financeBulkstockPledgeRateDetailList);

			detail.setEnterpriseGradeEvaluationDetail(eged);
		}

		if (enterpriseGrade != null) {
			BeanUtils.copyProperties(enterpriseGrade, detail);
			detail.setEffectiveEndDate(DateUtil.formatDate(enterpriseGrade.getEffectiveEndDate()));
			detail.setEffectiveStartDate(DateUtil.formatDate(enterpriseGrade.getEffectiveStartDate()));
			if (detail.getCreditLine().subtract(detail.getCreditAlready()).compareTo(BigDecimal.ZERO) < 0) {
				detail.setCreditRemaining(BigDecimal.ZERO);
			} else {
				detail.setCreditRemaining(detail.getCreditLine().subtract(detail.getCreditAlready()));
			}
		}

		TemporaryCreditLineService temporaryCreditLineService = ComponentFactory.loadClass(TemporaryCreditLineService.class);
		TemporaryCreditLineEntity tce = temporaryCreditLineService.inquireTemporaryCreditLine(entity.getEnterpriseId());
		TemporaryCreditlineDetail tcd = new TemporaryCreditlineDetail();
		if (null != tce) {
			BeanUtils.copyProperties(tce, tcd);
			tcd.setEffectiveStartDate(DateUtil.formatDate(tce.getEffectiveStartDate()));
			tcd.setEffectiveEndDate(DateUtil.formatDate(tce.getEffectiveEndDate()));
		}
		detail.setTemporaryCreditlineDetail(tcd);

		UserCardInfoService userCardInfoService = ComponentFactory.loadClass(UserCardInfoService.class);
		List<UserCardInfoEntity> list = userCardInfoService.inquireUserCardInfoList(entity.getEnterpriseId());
		if (list.isEmpty()) {
			detail.setIsCard(Boolean.FALSE);
		} else {
			detail.setIsCard(Boolean.TRUE);
			detail.setInterestRate(enterpriseGrade.getInterestRate());
			detail.setPenalInterest(enterpriseGrade.getPenalInterest());
		}

		//查询跟进人列表
		List<FinanceFollowPersonEntity> followPersonList = financeFollowPersonService.inquireFinanceFollowPersonList(entity.getEnterpriseId());
		List<FinanceFollowPersonDetail> followPersonResults = new ArrayList<>();
		String ffpes = "";
		for (FinanceFollowPersonEntity financeFollowPersonEntity: followPersonList) {
			ffpes = "," + ffpes;
			FinanceFollowPersonDetail d = new FinanceFollowPersonDetail();
			BeanUtils.copyProperties(financeFollowPersonEntity, d);
			followPersonResults.add(d);
		}
		if (StringUtils.isNotBlank(ffpes)) {
			detail.setClientManager(ffpes.substring(1));
		}
		detail.setFollowPersonList(followPersonResults);

		//查询佣金比例分配信息
		List<CommissionScaleTableEntity> commissionScaleTableList = commissionScaleTableService.inquireCommissionScaleTable(entity.getEnterpriseId(), null);
		List<CommissionScaleTable> commissionScaleTableResults = new ArrayList<>();
		for (CommissionScaleTableEntity cste : commissionScaleTableList) {
			CommissionScaleTable cst = new CommissionScaleTable();
			BeanUtils.copyProperties(cste, cst);
			commissionScaleTableResults.add(cst);
		}
		detail.setCommissionScaleTableList(commissionScaleTableResults);

		return detail;
	}

	public static EnterpriseDetail toEnterpriseModify(EnterpriseModifyEntity entity) {
		FinanceFollowPersonService financeFollowPersonService = ComponentFactory.loadClass(FinanceFollowPersonService.class);
		CommissionScaleTableService commissionScaleTableService = ComponentFactory.loadClass(CommissionScaleTableService.class);
		EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
		FinanceBulkstockPledgeRateService financeBulkstockPledgeRateService = ComponentFactory.loadClass(FinanceBulkstockPledgeRateService.class);
		BulkstockInfoService bulkstockInfoService = ComponentFactory.loadClass(BulkstockInfoService.class);
		EnterpriseDetail detail = new EnterpriseDetail();
		BeanUtils.copyProperties(entity, detail);
		//		detail.setAuditState(entity.getAuditState().ordinal());
		//		detail.setState(entity.getState().ordinal());
		//		detail.setAuthentication(entity.getAuthentication().ordinal());
		//		detail.setApproveType(entity.getApproveType().ordinal());
		if (entity.getEnterpriseType() != null) {
			detail.setEnterpriseType(entity.getEnterpriseType().ordinal());
		}

		EnterpriseCodeService enterpriseCodeService = ComponentFactory.loadClass(EnterpriseCodeService.class);
		EnterpriseCodeEntity enterpriseCode = enterpriseCodeService.inquireEnterpriseCodeDetail(null, entity.getEnterpriseId());

		if (null != enterpriseCode) {
			detail.setEnterpriseCode(enterpriseCode.getEnterpriseCode());
		}

		if (StringUtils.isNotBlank(entity.getBelongsEnterpriseCode())) {
			EnterpriseCodeEntity ece = enterpriseCodeService.inquireEnterpriseCodeDetail(entity.getBelongsEnterpriseCode(), null);
			if (ece != null) {
				EnterpriseModifyEntity ee = enterpriseService.inquireAuthentication(ece.getEnterpriseId());
				detail.setBelongsEnterprise(ee.getEnterpriseName());
			}
		}

		EnterpriseGradeService enterpriseGradeService = ComponentFactory.loadClass(EnterpriseGradeService.class);
		EnterpriseGradeEntity enterpriseGrade = enterpriseGradeService.inquireEnterpriseGrade(entity.getEnterpriseId());
		EnterpriseGradeEvaluationService enterpriseGradeEvaluationService = ComponentFactory.loadClass(EnterpriseGradeEvaluationService.class);
		EnterpriseGradeEvaluationEntity egee = null;
		if (enterpriseGrade != null) {
			egee = enterpriseGradeEvaluationService.inquireEnterpriseGradeEvaluationDetail(enterpriseGrade.getGradeEvaluationId());
			EnterpriseGradeDetail enterpriseGradeDetail = new EnterpriseGradeDetail();
			BeanUtils.copyProperties(enterpriseGrade, enterpriseGradeDetail);
			enterpriseGradeDetail.setCreditLineRemains(enterpriseGrade.getCreditLine().subtract(enterpriseGrade.getCreditAlready()));
			enterpriseGradeDetail.setEffectiveEndDate(DateUtil.formatDate(enterpriseGrade.getEffectiveEndDate()));
			enterpriseGradeDetail.setEffectiveStartDate(DateUtil.formatDate(enterpriseGrade.getEffectiveStartDate()));
			detail.setEnterpriseGradeDetail(enterpriseGradeDetail);
		}
		EnterpriseGradeEvaluationDetail eged = new EnterpriseGradeEvaluationDetail();
		if (egee != null) {
			eged = EnterpriseCastor.toEnterpriseGradeEvaluationDetail(egee);
			List<FinanceBulkstockPledgeRateEntity> financeBulkstockPledgeRateList = financeBulkstockPledgeRateService.inquireFinanceBulkstockPledgeRateList(egee.getGradeEvaluationId());
			List<FinanceBulkstockPledgeRateDetail> financeBulkstockPledgeRateDetailList = new ArrayList<>();
			for (FinanceBulkstockPledgeRateEntity fbpre : financeBulkstockPledgeRateList) {
				FinanceBulkstockPledgeRateDetail d = new FinanceBulkstockPledgeRateDetail();
				BeanUtils.copyProperties(fbpre, d);
				d.setEffectiveEndDate(DateUtil.formatDate(fbpre.getEffectiveEndDate()));
				d.setEffectiveStartDate(DateUtil.formatDate(fbpre.getEffectiveStartDate()));
				BulkstockInfoEntity bie = bulkstockInfoService.inquireBulkstockInfo(fbpre.getBulkstockInfoId());
				d.setBulkstockName(bie.getBulkstockName());
				d.setSpecificationName(bie.getSpecificationName());
				d.setProductName(fbpre.getProductName());
				d.setProductType((fbpre.getProductType() == null ? ProductTypeEnum.UNKNOWN.ordinal() : fbpre.getProductType().ordinal()));
				financeBulkstockPledgeRateDetailList.add(d);
			}
			eged.setFinanceBulkstockPledgeRateList(financeBulkstockPledgeRateDetailList);

			detail.setEnterpriseGradeEvaluationDetail(eged);
		}

		if (enterpriseGrade != null) {
			BeanUtils.copyProperties(enterpriseGrade, detail);
			detail.setEffectiveEndDate(DateUtil.formatDate(enterpriseGrade.getEffectiveEndDate()));
			detail.setEffectiveStartDate(DateUtil.formatDate(enterpriseGrade.getEffectiveStartDate()));
			if (detail.getCreditLine().subtract(detail.getCreditAlready()).compareTo(BigDecimal.ZERO) < 0) {
				detail.setCreditRemaining(BigDecimal.ZERO);
			} else {
				detail.setCreditRemaining(detail.getCreditLine().subtract(detail.getCreditAlready()));
			}
		}

		TemporaryCreditLineService temporaryCreditLineService = ComponentFactory.loadClass(TemporaryCreditLineService.class);
		TemporaryCreditLineEntity tce = temporaryCreditLineService.inquireTemporaryCreditLine(entity.getEnterpriseId());
		TemporaryCreditlineDetail tcd = new TemporaryCreditlineDetail();
		if (null != tce) {
			BeanUtils.copyProperties(tce, tcd);
			tcd.setEffectiveStartDate(DateUtil.formatDate(tce.getEffectiveStartDate()));
			tcd.setEffectiveEndDate(DateUtil.formatDate(tce.getEffectiveEndDate()));
		}
		detail.setTemporaryCreditlineDetail(tcd);

		UserCardInfoService userCardInfoService = ComponentFactory.loadClass(UserCardInfoService.class);
		List<UserCardInfoEntity> list = userCardInfoService.inquireUserCardInfoList(entity.getEnterpriseId());
		if (list.isEmpty()) {
			detail.setIsCard(Boolean.FALSE);
		} else {
			detail.setIsCard(Boolean.TRUE);
			detail.setInterestRate(enterpriseGrade.getInterestRate());
			detail.setPenalInterest(enterpriseGrade.getPenalInterest());
		}

		//查询跟进人列表
		List<FinanceFollowPersonEntity> followPersonList = financeFollowPersonService.inquireFinanceFollowPersonList(entity.getEnterpriseId());
		List<FinanceFollowPersonDetail> followPersonResults = new ArrayList<>();
		String ffpes = "";
		for (FinanceFollowPersonEntity financeFollowPersonEntity : followPersonList) {
			ffpes = "," + ffpes;
			FinanceFollowPersonDetail d = new FinanceFollowPersonDetail();
			BeanUtils.copyProperties(financeFollowPersonEntity, d);
			followPersonResults.add(d);
		}
		if (StringUtils.isNotBlank(ffpes)) {
			detail.setClientManager(ffpes.substring(1));
		}
		detail.setFollowPersonList(followPersonResults);

		//查询佣金比例分配信息
		List<CommissionScaleTableEntity> commissionScaleTableList = commissionScaleTableService.inquireCommissionScaleTable(entity.getEnterpriseId(), null);
		List<CommissionScaleTable> commissionScaleTableResults = new ArrayList<>();
		for (CommissionScaleTableEntity cste : commissionScaleTableList) {
			CommissionScaleTable cst = new CommissionScaleTable();
			BeanUtils.copyProperties(cste, cst);
			commissionScaleTableResults.add(cst);
		}
		detail.setCommissionScaleTableList(commissionScaleTableResults);

		return detail;
	}

	public static EnterpriseDetail toEnterpriseAccounts(EnterpriseAccountEntity entity) {
		EnterpriseDetail detail = new EnterpriseDetail();
		BeanUtils.copyProperties(entity, detail);
		return detail;
	}

	public static void syncCustInfo(EnterpriseEntity enterprise) {
		String url = ConfigPropertites.getProperties("finance.syncCustInfo.service");
		Map<String, String> params = new HashMap<>();

		params.put("type", "1");
		params.put("customer.finance_relate_id", enterprise.getEnterpriseId().toString());
		params.put("customer.customerName", enterprise.getEnterpriseName());
		params.put("customer.shortName", enterprise.getEnterpriseShortName());
		params.put("customer.telephone", enterprise.getMobile());
		params.put("customer.legal", enterprise.getLegalPerson());
		params.put("customer.business", enterprise.getBusinessScope());
		params.put("customer.province", enterprise.getProvince());
		params.put("customer.city", enterprise.getCity());
		params.put("customer.area", enterprise.getDistrict());
		params.put("customer.address", enterprise.getAddress());
		params.put("customer.registerNo", enterprise.getRegNo());

		params.put("account.accountname", enterprise.getAccountHolder());
		params.put("account.bank", enterprise.getDepositBank());
		params.put("account.bankAccount", enterprise.getCreditCard());

		params.put("contact.name", enterprise.getLegalPerson());
		params.put("contact.mobile", enterprise.getMobile());

		params.put("address.province", enterprise.getProvince());
		params.put("address.city", enterprise.getCity());
		params.put("address.area", enterprise.getDistrict());
		params.put("address.address", enterprise.getAddress());
		params.put("address.contact_name", enterprise.getLegalPerson());
		params.put("address.contact_num", enterprise.getMobile());

		String responseText = HttpClientHelper.post(url, params);
		logger.info("url:" + url);
		logger.info("params:" + params);
		logger.info("responseText:" + responseText);

		if (StringUtils.isBlank(responseText)){
			throw new BusinessException("同步失败");
		}
	}

	public static void syncCustInfo(SupplierEntity supplier) {
		String url = ConfigPropertites.getProperties("finance.syncCustInfo.service");
		Map<String, String> params = new HashMap<>();

		params.put("type", "0");
		params.put("customer.finance_relate_id", supplier.getSupplierId().toString());
		params.put("customer.customerName", supplier.getSupplierName());
		params.put("customer.shortName", supplier.getSupplierNameSimple());
		params.put("customer.telephone", supplier.getTaxpayerPhone());
		params.put("customer.legal", supplier.getLinkMan());
		params.put("customer.province", supplier.getWorkAddressProvince());
		params.put("customer.city", supplier.getWorkAddressCity());
		params.put("customer.area", supplier.getWorkAddressDistrict());
		params.put("customer.address", supplier.getDetailedAddress());
		params.put("customer.registerNo", supplier.getRegisterNumber());

		params.put("account.accountname", supplier.getLinkMan());
		params.put("account.bank", supplier.getBankName());
		params.put("account.bankAccount", supplier.getBankCard());

		params.put("contact.name", supplier.getLinkMan());
		params.put("contact.mobile", supplier.getTaxpayerPhone());

		params.put("address.province", supplier.getWorkAddressProvince());
		params.put("address.city", supplier.getWorkAddressCity());
		params.put("address.area", supplier.getWorkAddressDistrict());
		params.put("address.address", supplier.getDetailedAddress());
		params.put("address.contact_name", supplier.getLinkMan());
		params.put("address.contact_num", supplier.getTaxpayerPhone());

		String responseText = HttpClientHelper.post(url, params);
		logger.info("url:" + url);
		logger.info("params:" + params);
		logger.info("responseText:" + responseText);

		if (StringUtils.isBlank(responseText)) {
			throw new BusinessException("同步失败");
		}
	}

	public static EnterpriseBaseDetail toEnterpriseBaseDetail(EnterpriseEntity entity) {
		EnterpriseBaseDetail detail = new EnterpriseBaseDetail();
		BeanUtils.copyProperties(entity, detail);
		detail.setAuditState(entity.getAuditState().ordinal());
		detail.setState(entity.getState().ordinal());
		detail.setAuthentication(entity.getAuthentication().ordinal());
		if(entity.getFinalState() !=null){
			detail.setFinalState(entity.getFinalState().intValue());
		}
		EnterpriseGradeService enterpriseGradeService = ComponentFactory.loadClass(EnterpriseGradeService.class);
		EnterpriseGradeEntity enterpriseGrade = enterpriseGradeService.inquireEnterpriseGrade(entity.getEnterpriseId());
		if (enterpriseGrade != null) {
			BeanUtils.copyProperties(enterpriseGrade, detail);
		}
		try {
			if (DateUtils.daysBetween(DateUtils.now(), DateUtils.fromDateInt(enterpriseGrade.getEffectiveEndDate())) < 0) {
				detail.setGradeDays(0);
			} else {
				detail.setGradeDays(DateUtils.daysBetween(DateUtils.now(), DateUtils.fromDateInt(enterpriseGrade.getEffectiveEndDate())));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		FinanceFollowPersonService financeFollowPersonService = ComponentFactory.loadClass(FinanceFollowPersonService.class);
		//查询跟进人列表
		List<FinanceFollowPersonEntity> followPersonList = financeFollowPersonService.inquireFinanceFollowPersonList(entity.getEnterpriseId());
		StringBuffer ffpes = new StringBuffer("");
		for (FinanceFollowPersonEntity ffpe: followPersonList) {
			ffpes.append(ffpe.getFollowName()).append(",");
		}
		if (ffpes.length() > 0) {
			ffpes.deleteCharAt(ffpes.length() - 1);
		}
		detail.setFollowPersons(ffpes.toString());
		
		detail.setAuditStateView(entity.getAuditState().getValue());
		detail.setCreateDateTimeStr(DateUtil.formatDate(entity.getCreateDateTime()));
		return detail;
	}
	
	public static DaysEarningBaseDetail toDaysEarningBaseDetail(DaysEarningEntity entity) {
		DaysEarningBaseDetail detail = new DaysEarningBaseDetail();
		detail.setDaysEarningId(entity.getDaysEarningId());
		detail.setDaysEarningNumber(entity.getDaysEarningNumber());
		detail.setEarningDate(DateUtil.formatDate(entity.getEarningDate()));
		detail.setFinanceId(entity.getFinanceId());
		detail.setFinanceNumber(entity.getFinanceNumber());
		detail.setEnterpriseId(entity.getEnterpriseId());
		detail.setEnterpriseName(entity.getEnterpriseName());
		detail.setProductType(entity.getProductType().ordinal());
		detail.setPayMethod(entity.getPayMethod());
		detail.setInterestRate(entity.getInterestRate());
		detail.setBulkstockValuation(entity.getBulkstockValuation());
		detail.setInterest(entity.getInterest());
		detail.setLateFee(entity.getLateFee());
		detail.setFinanceState(entity.getFinanceState().ordinal());
		detail.setCreator(entity.getCreator());
		detail.setEnabled(entity.getEnabled()==true?new Integer(1):new Integer(0));
		detail.setCreateDt(DateUtil.formatDate(entity.getCreateDt()));
		detail.setUpdateDt(DateUtil.formatDate(entity.getUpdateDt()));
		detail.setFinanceAmount(entity.getFinanceAmount());
		//detail.setInterestDate(DateUtils.fromDateInt(entity.getInterestDate().intValue()));
		if(entity.getInterestDate() >0){
			String interestDate= String.valueOf(entity.getInterestDate());
			String strs=interestDate.substring(0, 4);
			String strs2=interestDate.substring(4, 6);
			String daysfmonth=interestDate.substring(6, 8);
//			int daysfmonth=Integer.parseInt(interestDate.substring(6, 8));
			detail.setInterestDate(strs+"-"+strs2+"-"+daysfmonth);
		}
		if(entity.getLoanDate() >0){
			String loanDate= String.valueOf(entity.getLoanDate());
			String strs=loanDate.substring(0, 4);
			String strs2=loanDate.substring(4, 6);
			String strs3=loanDate.substring(6, 8);
			detail.setLoanDate(strs+"-"+strs2+"-"+strs3);
		}
		return detail;
	}

	public static EarningSummaryBaseDetail toEarningSummaryBaseDetail(EarningSummaryEntity entity) {
		EarningSummaryBaseDetail detail = new EarningSummaryBaseDetail();
		if(entity.getEarningSummaryId() !=null){
			detail.setEarningSummaryId(entity.getEarningSummaryId());
		}
		if(entity.getLoanAmount() !=null){
			detail.setLoanAmount(entity.getLoanAmount());
		}
		if(entity.getLoanAmountIncrease() !=null){
			detail.setLoanAmountIncrease(entity.getLoanAmountIncrease());
		}
		if(entity.getLoanAmountRiseFall() !=null){
			detail.setLoanAmountRiseFall(entity.getLoanAmountRiseFall());
		}
		if(entity.getYestodayEarning() !=null){
			detail.setYestodayEarning(entity.getYestodayEarning());
		}
		if(entity.getYestodayEarningIncrease() !=null){
			detail.setYestodayEarningIncrease(entity.getYestodayEarningIncrease());
		}
		if(entity.getYestodayEarningRiseFall() !=null){
			detail.setYestodayEarningRiseFall(entity.getYestodayEarningRiseFall());
		}
		if(entity.getTotalEarning() !=null){
			detail.setTotalEarning(entity.getTotalEarning());
		}
		if(entity.getTotalEarningIncrease() !=null){
			detail.setTotalEarningIncrease(entity.getTotalEarningIncrease());
		}
		if(entity.getTotalEarningRiseFall() !=null){
			detail.setTotalEarningRiseFall(entity.getTotalEarningRiseFall());
		}
		if(entity.getEnabled() !=null){
			detail.setEnabled(entity.getEnabled()==true?new Integer(1):new Integer(0));
		}
		if(entity.getCreateDt() !=null){
			detail.setCreateDt(DateUtil.formatDate(entity.getCreateDt()));
		}
		if(entity.getUpdateDt() !=null){
			detail.setUpdateDt(DateUtil.formatDate(entity.getUpdateDt()));
		}
		return detail;
	}
	
	public static EnterpriseBaseDetail toEnterpriseBaseDetails(EnterpriseModifyEntity entity) {
		try {
			EnterpriseBaseDetail detail = new EnterpriseBaseDetail();
			BeanUtils.copyProperties(entity, detail);
			detail.setAuditState(entity.getAuditState().ordinal());
			detail.setState(entity.getState().ordinal());

			EnterpriseGradeService enterpriseGradeService = ComponentFactory.loadClass(EnterpriseGradeService.class);
			//			EnterpriseGradeEntity enterpriseGrade = enterpriseGradeService.inquireEnterpriseGrade(entity.getEnterpriseId());
			EnterpriseModifyEntity enterpriseGrade = enterpriseGradeService.queryEnterpriseGrade(entity.getAuthenticationId());
			if (enterpriseGrade != null) {
				BeanUtils.copyProperties(enterpriseGrade, detail);
			}
			return detail;
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException("企业信息生成失败");
		}
	}

	public static EnterpriseBaseDetail toAccountBaseDetails(EnterpriseAccountEntity entity) {
		try {
			EnterpriseBaseDetail detail = new EnterpriseBaseDetail();
			BeanUtils.copyProperties(entity, detail);

			EnterpriseGradeService enterpriseGradeService = ComponentFactory.loadClass(EnterpriseGradeService.class);
			EnterpriseAccountEntity enterpriseGrade = enterpriseGradeService.queryAccountGrade(entity.getAccountId());
			if (enterpriseGrade != null) {
				BeanUtils.copyProperties(enterpriseGrade, detail);
			}
			return detail;
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException("企业信息生成失败");
		}
	}

	public static EnterpriseBaseDetail toBankcodeDetails(BankcodeEntity entity) {
		try {
			EnterpriseBaseDetail detail = new EnterpriseBaseDetail();
			BeanUtils.copyProperties(entity, detail);
			EnterpriseGradeService enterpriseGradeService = ComponentFactory.loadClass(EnterpriseGradeService.class);
			BankcodeEntity enterpriseGrade = enterpriseGradeService.queryBankcodeGrade(entity.getLineNumberId());
			if (enterpriseGrade != null) {
				BeanUtils.copyProperties(enterpriseGrade, detail);
			}
			return detail;
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException("银行行号信息生成失败");
		}
	}

	/** 企业等级评估 */
	public static EnterpriseGradeEvaluationEntity fromEnterpriseGradeEvaluation(EnterpriseGradeEvaluation params) {
		EnterpriseGradeEvaluationService enterpriseGradeEvaluationService = ComponentFactory.loadClass(EnterpriseGradeEvaluationService.class);
		EnterpriseGradeEvaluationEntity entity = enterpriseGradeEvaluationService.inquireEnterpriseGradeEvaluationDetail(params.getGradeEvaluationId());
		if (entity == null) {
			entity = new EnterpriseGradeEvaluationEntity();
		}
		BeanUtils.copyProperties(params, entity);
		entity.setOperator(FinanceSessionContext.getAccountFullName());
		entity.setEffectiveEndDate(DateUtil.parseDateInt(params.getEffectiveEndDate()));
		entity.setEvaluationDateTime(DateUtil.now());
		if (StringUtils.isBlank(params.getEffectiveStartDate())) {
			entity.setEffectiveStartDate(DateUtil.parseDateInt(params.getEffectiveStartDate()));
		} else {
			entity.setEffectiveStartDate(DateUtil.todayInt());
		}

		if (params.getMemberGradeId() != null) {
			MemberGradeService memberGradeService = ComponentFactory.loadClass(MemberGradeService.class);
			MemberGradeEntity memberGrade = memberGradeService.inquireMemberGrade(params.getMemberGradeId());
			entity.setGradeName(memberGrade.getGradeName());
			entity.setEvaluationTemplate(memberGrade.getEvaluationTemplate());
		}
		return entity;
	}

	/** 企业等级评估 */
	public static EnterpriseGradeEvaluationDetail toEnterpriseGradeEvaluationDetail(EnterpriseGradeEvaluationEntity entity) {
		EnterpriseGradeEvaluationDetail info = new EnterpriseGradeEvaluationDetail();
		FinanceBulkstockPledgeRateService financeBulkstockPledgeRateService = ComponentFactory.loadClass(FinanceBulkstockPledgeRateService.class);
		BulkstockInfoService bulkstockInfoService = ComponentFactory.loadClass(BulkstockInfoService.class);
		BeanUtils.copyProperties(entity, info);
		info.setEffectiveEndDate(DateUtil.formatDate(entity.getEffectiveEndDate()));
		info.setEffectiveStartDate(DateUtil.formatDate(entity.getEffectiveStartDate()));
		if (entity.getAuditState() != null) {
			info.setAuditState(entity.getAuditState().ordinal());
		}
		if(entity.getFinalState() !=null){
			info.setFinalState(entity.getFinalState().intValue());
		}
		EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
		EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(entity.getEnterpriseId());
		if (enterprise != null) {
			info.setEnterpriseName(enterprise.getEnterpriseName());
		}
		List<FinanceBulkstockPledgeRateEntity> financeBulkstockPledgeRateList = financeBulkstockPledgeRateService.inquireFinanceBulkstockPledgeRateList(entity.getGradeEvaluationId());
		List<FinanceBulkstockPledgeRateDetail> financeBulkstockPledgeRateDetailList = new ArrayList<>();
		for (FinanceBulkstockPledgeRateEntity fbpre : financeBulkstockPledgeRateList) {
			FinanceBulkstockPledgeRateDetail d = new FinanceBulkstockPledgeRateDetail();
			BeanUtils.copyProperties(fbpre, d);
			d.setEffectiveEndDate(DateUtil.formatDate(fbpre.getEffectiveEndDate()));
			d.setEffectiveStartDate(DateUtil.formatDate(fbpre.getEffectiveStartDate()));
			BulkstockInfoEntity bie = bulkstockInfoService.inquireBulkstockInfo(fbpre.getBulkstockInfoId());
			d.setBulkstockName(bie.getBulkstockName());
			d.setSpecificationName(bie.getSpecificationName());
			d.setProductName(fbpre.getProductName());
			d.setProductType((fbpre.getProductType() == null ? ProductTypeEnum.UNKNOWN.ordinal() : fbpre.getProductType().ordinal()));
			financeBulkstockPledgeRateDetailList.add(d);
		}
		info.setFinanceBulkstockPledgeRateList(financeBulkstockPledgeRateDetailList);
		return info;
	}

	/** 临时额度评估 */
	public static TemporaryCreditlineEvaluationEntity fromTemporaryCreditlineEvaluationDetail(TemporaryCreditlineEvaluation params) {
		TemporaryCreditlineEvaluationService temporaryCreditlineEvaluationService = ComponentFactory.loadClass(TemporaryCreditlineEvaluationService.class);
		TemporaryCreditlineEvaluationEntity entity = null;
		if (params.getTemporaryCreditlineEvaluationId() != null) {
			entity = temporaryCreditlineEvaluationService.inquireTemporaryCreDitlineEvaluation(params.getTemporaryCreditlineEvaluationId());
		}
		if (entity == null) {
			entity = new TemporaryCreditlineEvaluationEntity();
		}
		BeanUtils.copyProperties(params, entity);
		entity.setOperator(FinanceSessionContext.getAccountFullName());
		entity.setEffectiveEndDate(DateUtil.parseDateInt(params.getEffectiveEndDate()));
		entity.setEvaluationDateTime(DateUtil.now());
		if (StringUtils.isBlank(params.getEffectiveStartDate())) {
			entity.setEffectiveStartDate(DateUtil.parseDateInt(params.getEffectiveStartDate()));
		} else {
			entity.setEffectiveStartDate(DateUtil.todayInt());
		}
		return entity;
	}

	/** 临时额度评估 */
	public static TemporaryCreditlineEvaluationDetail toTemporaryCreditlineEvaluationDetail(TemporaryCreditlineEvaluationEntity entity) {
		EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
		EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(entity.getEnterpriseId());
		TemporaryCreditlineEvaluationDetail info = new TemporaryCreditlineEvaluationDetail();
		BeanUtils.copyProperties(entity, info);
		info.setEffectiveEndDate(DateUtil.formatDate(entity.getEffectiveEndDate()));
		info.setEffectiveStartDate(DateUtil.formatDate(entity.getEffectiveStartDate()));
		info.setAuditState(entity.getAuditState().ordinal());
		if(entity.getFinalState() !=null){
			info.setFinalState(entity.getFinalState().intValue());
		}
		if (enterprise != null) {
			info.setEnterpriseName(enterprise.getEnterpriseName());
		} else {

		}
		return info;
	}

	/** 企业跟进人信息 */
	public static FinanceFollowPersonDetail toFinanceFollowPersonDetail(FinanceFollowPersonEntity entity) {
		FinanceFollowInfoService financeFollowInfoService = ComponentFactory.loadClass(FinanceFollowInfoService.class);
		List<FinanceFollowInfoEntity> ffies = financeFollowInfoService.inquireFinanceFollowInfoList(entity.getFollowId());
		List<FinanceFollowInfoDetail> ffids = new ArrayList<>();
		for (FinanceFollowInfoEntity ffie : ffies) {
			FinanceFollowInfoDetail ffid = new FinanceFollowInfoDetail();
			BeanUtils.copyProperties(ffie, ffid);
			ffids.add(ffid);
		}
		FinanceFollowPersonDetail detail = new FinanceFollowPersonDetail();
		BeanUtils.copyProperties(entity, detail);
		detail.setFollowInfoList(ffids);
		return detail;
	}

}
